/**
 * 
 */
package edu.cornell.cs.lsi.projone.common.util;

import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

public class AppUtil {

	private final static Log LOGGER = LogFactoryImpl.getLog(AppUtil.class);

	private static DatagramSocket socket = null;
	private static int port = -1;

	public static int getCallID() {

		Random generator = new Random();
		int call = generator.nextInt();
		return call;
	}

	public static DatagramSocket getDatagramSkt() {
		if (socket == null) {
			try {
				socket = new DatagramSocket();
				port = socket.getLocalPort();
			} catch (SocketException e) {
				LOGGER.error("AppUtil : error while creating a socket object");
				e.printStackTrace();
			}
		} else {
			return socket;
		}
		return null;
	}

	public static int getPort() {
		if (port != -1) {
			return port;
		}
		// DatagramSocket skt = null;
		// try {
		// skt = new DatagramSocket();
		getDatagramSkt();
		// port = skt.getLocalPort();
		// skt.close();
		// } catch (SocketException e) {
		// e.printStackTrace();
		// }
		return port;
	}

}
